﻿using System;
using System.Linq;
using Football.Models;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace Football.ViewModels
{
    public class GameViewModel
    {
        public string PlayerCountTitle { get; set; }
        public bool HasActivePlayers { get; set; }
        public bool HasInactivePlayers { get; set; }
        public string StartDate { get; set; }
        public string StartTime { get; set; }

        [DisplayName("Player Name")]
        [UIHint("Enter a the player's name.")]
        [Required(ErrorMessage = "To add a new player, enter a name.")]
        public string NewPlayerName { get; set; }

        public IEnumerable<GamePlayerIndex> GamePlayerList { get; set; }
        
        public GameViewModel() { }

        public GameViewModel(Game game)
        {
            this.StartDate = String.Format("{0:dddd d MMMM}", game.GameStartDate);

            this.StartTime = String.Format("{0:h tt}", game.GameStartDate);

            var activePlayerlist = game.GamePlayers.OrderBy(p => p.CreationDate)
               .Where(player => player.GamePlayerStatusId == 1)
               .Select((player, index) => new GameViewModel.GamePlayerIndex(player, index));

            var inactivePlayerlist = game.GamePlayers.OrderBy(p => p.CreationDate)
                .Where(player => player.GamePlayerStatusId == 2)
             .Select((player, index) => new GameViewModel.GamePlayerIndex(player, index));

            var playerList = activePlayerlist.Union(inactivePlayerlist).OrderBy(player => player.GamePlayer.CreationDate);//.ToList();

            switch (activePlayerlist.Count())
            {
                case 1:
                    this.PlayerCountTitle = "1 person so far!";
                    break;
                default:
                    this.PlayerCountTitle = activePlayerlist.Count() + " people so far!";
                    break;
            }

            this.HasActivePlayers = activePlayerlist.Count() > 0 ? true : false;
            this.HasInactivePlayers = inactivePlayerlist.Count() > 0 ? true : false;
            this.GamePlayerList = playerList;

        }

        public class GamePlayerIndex
        {
            public int Index { get; set; }
            public GamePlayer GamePlayer { get; set; }

            public GamePlayerIndex(GamePlayer gamePlayer, int index)
            {
                this.Index = index + 1;
                this.GamePlayer = gamePlayer;
            }
        }
    }

}